package com.dz.lm.web.controller.newuser.usercollection;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.domain.talkcomment.TalkComment;
import com.dz.lm.domain.usercollection.UserCollection;
import com.dz.lm.service.usercollection.UserCollectionService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * 用户收藏控制层
 *
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/12 21:19;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userCollection", produces = "application/json")
public class UserCollectionController extends BasicControl {
    @Autowired
    private UserCollectionService userCollectionService;

    /**
     * 操作收藏
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //1产品
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 1;
        //1-收藏2隐藏3-举报
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        //用户
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //对象id
        Integer objId = param.get("objId") != null ? Integer.parseInt(param.get("objId").toString()) : 0;
        //备注
        String remark = param.get("remark") != null ? param.get("remark").toString() : "";

        if (type == 0 || userId == 0 || objId == 0 || objId == 0) {
            message = "参数有误";
            result = "02";
        } else {
            int i = userCollectionService.add(userId, type, status, objId, remark);
            if (i > 0) {
                switch (type){
                    case 1://产品
                        switch (status){
                            case 1:
                                message = "收藏成功";
                                break;
                            case 2:
                                message = "隐藏成功";
                                break;
                            case 3:
                                message = "举报成功";
                                break;
                        }
                        break;
                    case 2://用户
                        switch (status){
                            case 1:
                                message = "关注成功";
                                break;
                            case 2:
                                message = "隐藏成功";
                                break;
                            case 3:
                                message = "举报成功";
                                break;
                        }
                        break;
                }
                result = "00";
            }
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 查询收藏
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/query")
    public Map query(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //1产品2人员
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 1;
        //1-收藏2隐藏3-举报
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        //
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //1 --产品id 2--用户id
        Integer objId = param.get("objId") != null ? Integer.parseInt(param.get("objId").toString()) : 0;

        Integer page = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;

        if ( type == 0) {
            message = "参数有误";
            result = "02";
        } else {
            data = userCollectionService.query(page+1, rows, userId, objId,type, status);
            message = "操作成功";
            result = "00";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 删除收藏
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //主键
        Integer collectId = param.get("collectId") != null ? Integer.parseInt(param.get("collectId").toString()) : 0;
        //1产品
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 1;
        //1-收藏2隐藏3-举报
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 0;
        //对象id
        Integer objId = param.get("objId") != null ? Integer.parseInt(param.get("objId").toString()) : 0;

        int deltet = userCollectionService.deltet(collectId, Integer.parseInt(uId), objId, status, type);
        if (deltet > 0) {
            message = "删除成功";
            result = "00";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

}
